<?php
namespace Home\Controller;

use Utuo\ForegroundController;
use Utuo\Sign;
use Utuo\DBRW;

class MicroShowController extends ForegroundController {
    
    public $musicList = array(
        1 => array(
            'id' => 1,
            'name' => '人约黄昏后',
            'url' => 'http://res.iweshow.com.cn/music/snowdreams.mp3',
        ),
        2 => array(
            'id' => 2,
            'name' => '童话',
            'url' => 'http://res.iweshow.com.cn/music/tonghua.mp3',
        ),
        3 => array(
            'id' => 3,
            'name' => '问候',
            'url' => 'http://res.iweshow.com.cn/music/wenhou.mp3',
        ),
        4 => array(
            'id' => 4,
            'name' => '新年快乐',
            'url' => 'http://res.iweshow.com.cn/music/newyear3.mp3',
        ),
        5 => array(
            'id' => 5,
            'name' => '恭喜恭喜',
            'url' => 'http://res.iweshow.com.cn/music/newyear2.mp3',
        ),
        6 => array(
            'id' => 6,
            'name' => '发财发福中国年',
            'url' => 'http://res.iweshow.com.cn/music/newyear1.mp3',
        ),
    );
	
	public function _init() {
        $this->microShowConfig = M("MicroShowConfig")->where(array("cid"=>$this->cid))->find();
        $this->microShowTplList = M("MicroShowTpl")->select();
        $this->assign("microShowTplList", $this->microShowTplList);
        $this->assign("microShowConfig", $this->microShowConfig);
	}
	
	/**
	 * 展示页
	 */
	public function indexAction() {
		$id = I('id', 0, "intval"); // 微秀id
		DBRW::openAndClose(0);
		$microShow = M("MicroShow a")->where("a.cid=%d and a.id=%d", $this->cid, $id)->field("a.*")->find() or $this->error("微秀已不存在");
		$microShow['tplId'] or $microShow['tplId'] = 1;
		$myShowTpl = M("MicroShowTpl a")->where("a.id=%d", $microShow['tplId'])->find() or $this->ajaxError("背景不存在");
		$microShow['tplName'] = $myShowTpl['name'];
		$microShow['config'] = unserialize($microShow['config'] );
		$this->bindCoverImage($microShow);
		$this->bindCoverDesc($microShow);
		$this->assign("share", array(
		    'title' => $microShow['title'],
		    'link' => U('MicroShow/index', array('id'=>$id,'sid'=>$this->sid), false, true),
		    'imgUrl' => $microShow['coverImage'],
		    'desc' => $microShow['description'],
		));
		$this->setPageTitle($microShow['title']);
		$this->assign("gSound", $this->musicList[$microShow['musicId']]['url']);
		$this->assign("microShow", $microShow);
		$this->display("MicroShow/tpl/{$microShow['tplName']}");
	}
	
	private function bindCoverImage( &$microShow ) {
	    foreach (  $microShow['config'] as $v0) {
	        if ( $v0['type'] == 'image' ) {
	            $microShow['coverImage'] = $v0['url'];
	            break;
	        }
	    }
	}
	
	private function bindCoverDesc( &$microShow ) {
	    foreach (  $microShow['config'] as $v0) {
	        if ( $v0['type'] == 'text' ) {
	            $microShow['description'] = $v0['content'];
	            break;
	        }
	    }
	}
	
	/**
	 * 列表页
	 */
	public function listAction() {
		$myShowList = M("MicroShow a")->where("a.cid=%d and a.openId='%s'", $this->cid, $this->open_id)->field("a.*")->select();
		foreach ($myShowList as &$v) {
		    $v['config'] = unserialize($v['config'] );
		    $v['viewUrl'] =  __CONTROLLER__.'/'.(($v['status'] == 0 or $v['status'] == 1) ? "modify" : "index")."/sid/{$this->cid}/id/{$v['id']}";
		    $this->bindCoverImage($v);
		    $this->bindCoverDesc($v);
		}
		$this->setPageTitle("微秀列表");
		$this->assign("list", $myShowList);
		$this->display("MicroShow/list");
	}
	
	/**
	 * 编辑页
	 */
	public function modifyAction() {
		$id = I('id', 0, "intval"); // 微秀id
		DBRW::openAndClose(0);
		$microShow = M("MicroShow a")->where("a.cid=%d and a.id=%d", $this->cid, $id)->field("a.*")->find() or $this->error("微秀已不存在");
		$microShow['tplId'] or $microShow['tplId'] = 1;
		$myShowTpl = M("MicroShowTpl a")->where("a.id=%d", $microShow['tplId'])->find() or $this->ajaxError("背景不存在");
		$microShow['tplName'] = $myShowTpl['name'];
		$microShow['config'] = unserialize($microShow['config'] );
		$this->bindCoverImage($microShow);
		$this->bindCoverDesc($microShow);
		$this->setPageTitle($microShow['title']);
		$this->assign("share", array(
		    'title' => $microShow['title'],
		    'link' => U('MicroShow/index', array('id'=>$id,'sid'=>$this->sid), false, true),
		    'imgUrl' => $microShow['coverImage'],
		    'desc' => $microShow['description'],
		));
		$this->assign("gSound", $this->musicList[$microShow['musicId']]['url']);
		$this->assign("microShow", $microShow);
		$this->assign("mode", "edit");
		$this->assign("musicList", $this->musicList);
		$this->display("MicroShow/tpl/{$microShow['tplName']}");
	}
	
	/**
	 * 更换背景ajax提交
	 */
	public function changeBgAction() {
		$id = I('id', 0, "intval"); // 微秀id
		$tplId = I('tplId', 0, "intval"); // 模板id
		$myShowTpl = M("MicroShowTpl a")->where("a.id=%d", $tplId)->find() or $this->ajaxError("背景不存在");
		false !== M("MicroShow")->where("cid=%d and id=%d", $this->cid, $id)->setField("tplId", $tplId) and $this->ajaxSuccess("更换背景成功");
		$this->ajaxError("更换背景失败");
	}
	
	/**
	 * 更换背景ajax提交
	 */
	public function changeMusicAction() {
	    $id = I('id', 0, "intval"); // 微秀id
	    $musicId = I('musicId', 0, "intval"); // 音乐id
	    false !== M("MicroShow")->where("cid=%d and id=%d", $this->cid, $id)->setField("musicId", $musicId) and $this->ajaxSuccess("更换音乐成功");
	    $this->ajaxError("更换音乐失败");
	}
	
	/**
	 * 编辑内容ajax提交
	 */
	public function modifyPostAction() {
		$id = I('id', 0, "intval"); // 微秀id
		$title = I('title'); // 标题配置
		$config = I('config'); // 内容配置
		$microShow = M("MicroShow a")->where("a.cid=%d and a.id=%d and  a.openId='%s'", $this->cid, $id, $this->open_id)->field("a.*")->find();
		$microShow['config'] = unserialize($microShow['config'] );
		foreach ( $microShow['config'] as $k => &$v ) {
			if ( array_key_exists($k, $config) ) {
				if ( $v['type'] == 'text' ) {
					if ( empty($config[$k]['content']) ) {
						unset($microShow['config'][$k]);
					} else {
						$v['content'] = $config[$k]['content'];
					}
				} elseif ( $v['type'] == 'image' ) {
					if ( empty($config[$k]['url']) ) {
						unset($microShow['config'][$k]);
					} else {
						$v['url'] = $config[$k]['url'];
					}
				}
			} else {
				unset($microShow['config'][$k]);
			}
		}
		$microShow['title'] = $title;
		$microShow['config'] = serialize($microShow['config'] );
		false !== M("MicroShow")->save($microShow) and $this->ajaxSuccess("修改成功");
		$this->ajaxError("修改失败");
	}
	
	public function deletePostAction() {
	    $id = I('id', 0, "intval"); // 微秀id
	    $result  = M("MicroShow")->where("cid=%d and id=%d and openId='%s'", $this->cid, $id, $this->open_id)->delete() and $this->ajaxSuccess("删除成功");
	    $this->ajaxError("删除失败");
	}
	
	/**
	 * 发布
	 */
	public function publishPostAction() {
		
	}
	
	/**
	 * 链接查看
	 */
	public function urlAction() {
	    $t = time();
	    $openId = 'oJq-iuJuBa5YPwKDH1xd3WGPIXuE';
	    $sign = Sign::create( array("open_id"=>$openId, "_t"=>$t));
	    echo sprintf("http://w.wmfl.cn/index.php/Home/MicroShow/list/sid/6?open_id=%s&_s=%s&_t=%d", $openId, $sign, $t);
	}
	
}